package net.kem.android.positioner.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

import net.kem.android.positioner.MessageObject;
import net.kem.android.positioner.PositionerService;

import java.util.LinkedList;
import java.util.List;

public class ServerMessageSwitch extends ServerMessageElement {
	protected ImageView imageOn, imageOff;

	public ServerMessageSwitch(PositionerService.LocalBinder binder, Context context, int imageOffResouceId, int imageOnResouceId, MessageObject messageObject) {
		this(binder, context, (AttributeSet)null, imageOffResouceId, imageOnResouceId, messageObject);
	}
	public ServerMessageSwitch(PositionerService.LocalBinder binder, Context context, AttributeSet attrs, int imageOffResouceId, int imageOnResouceId, MessageObject messageObject) {
		super(binder, context, null, attrs, messageObject);

		// Instantiate an ImageView and define its properties
		imageOff = new ImageView(context);
		imageOff.setImageResource(imageOffResouceId);
		imageOff.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
		//imageOff.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		imageOn = new ImageView(context);
		imageOn.setImageResource(imageOnResouceId);
		imageOn.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
		//imageOn.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		boolean isOn = !messageObject.getState().equals("0");
		setChecked(isOn, false);
	}

//	public ServerMessageSwitch(Context context, AttributeSet attrs, int defStyle, ImageView image, String text) {
//		super(context, attrs, image);
//		view = new ToggleButton(context);
//		addView(view);
//	}

	public boolean isChecked() {
		boolean res = image == imageOn;
		return res;
	}

	public void setChecked(boolean checked, boolean doSend) {
		setImage(checked? imageOn: imageOff);
		setActive(checked);

		if(doSend) {
			sendServerMessage();
		}
	}

	protected void sendServerMessage() {
			List<MessageObject> objects = new LinkedList<MessageObject>();
			objects.add((MessageObject)namable);
			binder.sendSetObjects(objects);
	}
}